package com.ruoyi.web.controller.installation;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WaterUser;
import com.ruoyi.system.service.IWaterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户档案Controller
 *
 * @author ruoyi
 * @date 2025-09-22
 */
@RestController
@RequestMapping("/installation/waterUser")
public class WaterUserController extends BaseController
{
    @Autowired
    private IWaterUserService waterUserService;

    /**
     * 查询用户档案列表
     */
    @PreAuthorize("@ss.hasPermi('installation:waterUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(WaterUser waterUser)
    {
        startPage();
        List<WaterUser> list = waterUserService.selectWaterUserList(waterUser);
        return getDataTable(list);
    }

    /**
     * 导出用户档案列表
     */
    @PreAuthorize("@ss.hasPermi('installation:waterUser:export')")
    @Log(title = "用户档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WaterUser waterUser)
    {
        List<WaterUser> list = waterUserService.selectWaterUserList(waterUser);
        ExcelUtil<WaterUser> util = new ExcelUtil<WaterUser>(WaterUser.class);
        util.exportExcel(response, list, "用户档案数据");
    }

    /**
     * 获取用户档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('installation:waterUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(waterUserService.selectWaterUserById(id));
    }

    /**
     * 新增用户档案
     */
    @PreAuthorize("@ss.hasPermi('installation:waterUser:add')")
    @Log(title = "用户档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WaterUser waterUser)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        waterUser.setCreateBy(loginUser.getUsername());
        waterUser.setCompanyId(getDeptId() + "");
        String userId = waterUser.getUserId();
        if(StringUtils.isEmpty(userId)){
            userId = waterUserService.getMaxUserId();
        }
        waterUser.setUserId(userId);
        return toAjax(waterUserService.insertWaterUser(waterUser));
    }

    /**
     * 修改用户档案
     */
    @PreAuthorize("@ss.hasPermi('installation:waterUser:edit')")
    @Log(title = "用户档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WaterUser waterUser)
    {
        return toAjax(waterUserService.updateWaterUser(waterUser));
    }

    /**
     * 删除用户档案
     */
    @PreAuthorize("@ss.hasPermi('installation:waterUser:remove')")
    @Log(title = "用户档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(waterUserService.deleteWaterUserByIds(ids));
    }
}
